package com.zzusedating.androidapp;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.zzusedating.androidapp.customlayout.TitleBarLayout;
import com.zzusedating.androidapp.fragments.DiscoverFragment;
import com.zzusedating.androidapp.tools.BasicCallback;
import com.zzusedating.androidapp.tools.BasicHandlerCallback;
import com.zzusedating.androidapp.tools.ConstantsTools;
import com.zzusedating.androidapp.tools.MyApplication;

import org.json.JSONObject;

import java.util.Objects;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class TweetActivity extends BaseActivity {
    private static final String TAG = "TweetActivity";

    private static final int NOT_LOGIN = -1;
    private static final int SEND_SUCCESS = 1;
    private static final String NOT_LOGIN_ = "请先登录";

    private Editable content;
    private EditText editText;

    private BasicCallback callback;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tweet_layout);

        initCallback();
        editText = findViewById(R.id.Tweet_edit);
        TitleBarLayout titleBarLayout = findViewById(R.id.Tweet_title);
        titleBarLayout.setClickListenerTo(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content = editText.getText();
                if (TextUtils.isEmpty(content))
                    return;
                MyApplication.getClient().newCall(
                        new Request.Builder()
                                .get()
                                .url(String.format(ConstantsTools.TWEET_, content.toString()))
                                .build()
                ).enqueue(callback);
            }
        }, TitleBarLayout.TO_TEXT);
    }

    @Override
    protected void initCallback() {
        handler = new Handler(new BasicHandlerCallback() {
            @Override
            public boolean handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case NOT_LOGIN:
                        MyApplication.Toast(NOT_LOGIN_);
                        break;
                    case SEND_SUCCESS:
                        Intent intent = new Intent();
                        intent.putExtra(DiscoverFragment.MY_TWEET, content);
                        setResult(RESULT_OK, new Intent());
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        if (Objects.requireNonNull(imm).isActive() && getCurrentFocus() != null) {
                            if (getCurrentFocus().getWindowToken() != null) {
                                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                            }
                        }
                        finish();
                }
                return true;
            }
        });
        callback = new BasicCallback(handler) {
            @Override
            public void onResponse(Call call, @NonNull Response response) {
                handler.sendMessage(ConstantsTools.createMessage(SEND_SUCCESS));
            }
        };
    }

    @Override
    public void onBackPressed() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (Objects.requireNonNull(imm).isActive() && getCurrentFocus() != null) {
            if (getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        finish();
    }
}
